package com.xianbeiying.app.ckgl.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.widght.dashview.DashboardView;
import com.xianbeiying.app.ckgl.widght.dashview.HighlightCR;

import java.util.ArrayList;
import java.util.List;

/**
 * 实时信息数据适配
 */
public class RealTimeInfossAdapter extends BaseAdapter{
    private Context context;
    private String[] typename={"空气温度","空气湿度","土壤温度","土壤湿度","光照强度","二氧化碳","用水统计"};
//    private float[] id={0,90,180,270,360,-90,-180};
//    private float[] id={0,15,30,45,60,75,90};
//    private int [] id={90,105,120,135,150,165,180};
    private int [] imgView={R.mipmap.tw,R.mipmap.ty,R.mipmap.tw,R.mipmap.ty,R.mipmap.gz,R.mipmap.co2,R.mipmap.water};
    private String [] x={"℃","RH","℃","RH","Lux","ppm","m³"};
    private int [] min={0,0,0,0,20,0,0};
    private int [] max={60,100,60,100,20000,100,100};
    private int [] xdata;
    public RealTimeInfossAdapter(Context context, int [] xdata) {
        this.context = context;
        this.xdata=xdata;
    }
    @Override
    public int getCount() {
        return typename.length+1;
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_real_time_infoss, parent, false);
            holder = new ViewHolder();
            holder.ly_gjingjie=(LinearLayout) convertView.findViewById(R.id.ly_gjingjie);
            holder.ly_djingjie=(LinearLayout) convertView.findViewById(R.id.ly_djingjie);
            holder.tv_real_name=(TextView) convertView.findViewById(R.id.tv_real_name);
            holder.real_img=(ImageView) convertView.findViewById(R.id.real_img);
            holder.rl_real_time=(RelativeLayout) convertView.findViewById(R.id.rl_real_time);
            holder.dashboard_view=(DashboardView)convertView.findViewById(R.id.dashboard_view);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        if(position==typename.length){
            holder.ly_gjingjie.setVisibility(View.VISIBLE);
            holder.ly_djingjie.setVisibility(View.VISIBLE);
            holder.rl_real_time.setVisibility(View.GONE);
            holder.tv_real_name.setVisibility(View.GONE);
        }else{
            List<HighlightCR> highlight2 = new ArrayList<>();
            highlight2.add(new HighlightCR(170, 50, ContextCompat.getColor(context,R.color.rinfo_lv)));
            highlight2.add(new HighlightCR(210, 140, ContextCompat.getColor(context,R.color.rinfo_lan)));
            highlight2.add(new HighlightCR(330, 50,ContextCompat.getColor(context,R.color.rinfo_h)));
            holder.dashboard_view.setStripeHighlightColorAndRange(highlight2);
            holder.tv_real_name.setVisibility(View.VISIBLE);
            holder.rl_real_time.setVisibility(View.VISIBLE);
            holder.ly_gjingjie.setVisibility(View.GONE);
            holder.ly_djingjie.setVisibility(View.GONE);
            holder.tv_real_name.setText(typename[position]);
            holder.real_img.setImageResource(imgView[position]);
            holder.dashboard_view.setHeaderTitle(x[position]);
            holder.dashboard_view.setMinValue(min[position]);
            holder.dashboard_view.setMaxValue(max[position]);
            holder.dashboard_view.setBigSliceCount(5);
//            app:bigSliceCounts="5"
            holder.dashboard_view.setTextColor(ContextCompat.getColor(context,R.color.app_color));
//            app:textColors="@color/app_color"
            holder.dashboard_view.setHeaderRadius(25);
//            app:headerRadiuss="25dp"
//            android:layout_gravity="center_horizontal"
//            app:headerTitles="km/h"
//            app:maxValues="100"
            holder.dashboard_view.setMeasureTextSize(10);
//            app:measureTextSizes="10sp"
            holder.dashboard_view.setRadius(55);
//            app:radiuss="55dp"
            holder.dashboard_view.setSliceCountInOneBigSlice(0);
//            app:sliceCountInOneBigSlices="0"
            holder.dashboard_view.setRealTimeValue(0);
//            app:realTimeValues="0"
            holder.dashboard_view.setStartAngle(170);
//            app:startAngles="170"
            holder.dashboard_view.setStripeWidth(3);
//            app:stripeWidths="3dp"
            holder.dashboard_view.setSweepAngle(200);
//            app:sweepAngles="200"
            holder.dashboard_view.setRealTimeValue(xdata[position],true,500);
        }

        return convertView;
    }
    private class ViewHolder{
        RelativeLayout rl_real_time;
       LinearLayout ly_gjingjie,ly_djingjie;
       TextView tv_real_name;
       ImageView real_img;
        DashboardView dashboard_view;
    }
}
